echo "==============================================="
# 获取当前分支名并保存到变量
current_branch=$(git rev-parse --abbrev-ref HEAD)

# 输出当前分支名
echo "当前项目分支:$current_branch"

current_modify=0
# 检查本地是否有未提交的修改
if [[ -n $(git status --porcelain) ]];
then 
	current_modify=1
fi

echo "代码修改状态:$current_modify"
# 检查是否有未推送的提交
local_commits=$(git rev-list --count HEAD)
remote_commits=$(git rev-list --count @{u})

unpush_status=0
if [[ $local_commits -gt $remote_commits ]]; 
then
	unpush_status=1
fi
echo "代码PUSH状态:$unpush_status"

history_log=$(git log -n 30 --pretty=format:'"%ad =>%s"', --date format:"%y-%m-%d %H:%M:%S")
history_log="${history_log%?}"
echo "修改日志记录:[$history_log]"

last_log=$(git log -n 1 --pretty=format:'%h')

tagver="$current_branch"
tagver+="_$current_modify"
tagver+="_$unpush_status"
tagver+="_$last_log"
echo "==============================================="

echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "请选择一个项目:"
echo "1. 报考管家"
echo "2. 金榜如愿"
echo "3. 志愿星"
echo "4. 大民唐邦教育"
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

appId=''
appName=''
domainName=''
# 读取用户输入
while true
do
read -p "请输入数字: " choice

# 检查输入是否为空
if [ -z "$choice" ]; then
    echo "输入不能为空，请输入一个数字###"
elif [ "$choice" -eq 1 ]; then
     echo '配置:报考管家'
	 
	 appId='wx04a4ad5325758043'
	 appName='报考管家'
	 domainName='https://xcx.baokaoguanjia.com'
     break
elif [ "$choice" -eq 2 ]; then
    echo '配置:金榜如愿'
	
	appId='wxc6791a5cb7eebdb5'
	appName='金榜如愿'
	domainName='https://zym.jinbangruyuan.com'
    break
elif [ "$choice" -eq 3 ]; then
    echo '配置:志愿星'
	appId='wx04a4ad5325758043'
	appName='志愿星'
	domainName='https://xcx.chatgk.com'
    break
elif [ "$choice" -eq 4 ]; then
    echo '配置:大民唐邦教育'
	
	appId='未配置'
	appName='大民唐邦教育'
	domainName='https://dmtm.chatgk.cn'
    break
else
    echo "无效选择"
	continue
fi
done

curent_time=$(date +"%Y-%m-%d %H:%M:%S")
dev_user=$(git config user.name)


if [ "$dev_user" == "" ]; then
	echo "开发者获取失败."
	exit
fi

if [ "$curent_time" == "" ]; then
	echo "当前时间获取失败."
	exit
fi
	
if [ "$appId" == "" ]; then
	echo "APPID 未配置."
	exit
fi

if [ "$appName" == "" ]; then
	echo "APP 名称 未配置."
	exit
fi

if [ "$domainName" == "" ]; then
	echo "APP 访问域名 未配置."
	exit
fi


echo "export default {  user:'$dev_user',publish_time:'$curent_time',appId:'$appId',  name: '$appName',    domainName: '$domainName',historylog:[$history_log], lasttag:'$tagver'}" > common/manager/xcxinfo.js

echo "a.--------------- common/manager/xcxinfo.js 文件内容 >>>>>>>>>>>>>>>>>>"
cat common/manager/xcxinfo.js
echo ""

#echo "b.--------------- manifest.json 文件配置结果 >>>>>>>>>>>>>>>>>>"
echo "开始修改 manifest APP名称:$appName"
#echo "$appName"

cmdtxt="sed -i '' 's/\"name\": *\"[^\"]*\"/\"name\": \"$appName\"/' manifest.json"
#echo "$cmdtxt"
eval $cmdtxt

cmdtxt="grep \"\\\"name\\\": *\\\"$appName\\\"\" manifest.json | sed -n 's/.*\"\([^\"]*\)\".*/\1/p'"
#echo "$cmdtxt"
rst=$(eval $cmdtxt)
#echo "[$rst] | [$appName]"
if [ "$rst" != "$appName" ]; then
#if $rst != $appName; then
	echo "修改manifest -- APP名称 失败."
	exit
fi
echo "成功修改 manifest APP名称:$appName"


echo "开始修改 pages首页标题:$appName"
cmdtxt="sed -i '' 's/\"navigationBarTitleText\": *\"[^\"]*\", \/\/小程序首页标题,本行格式不要修改,重要重要./\"navigationBarTitleText\": \"$appName\", \\/\\/小程序首页标题,本行格式不要修改,重要重要./' pages.json"
#echo "$cmdtxt"
rst=$(eval $cmdtxt)
cmdtxt="grep \"\\\"navigationBarTitleText\\\": *\\\"$appName\\\", \\/\\/小程序首页标题,本行格式不要修改,重要重要.\" pages.json | sed -n 's/.*\"\([^\"]*\)\".*/\1/p'"
#echo "$cmdtxt"
rst=$(eval $cmdtxt)
#echo "[$rst] | [$appName]"
if [ "$rst" != "$appName" ]; then
#if $rst != $appName; then
	echo "修改pages -- 主页标题失败."
	exit
fi
echo "成功修改 pages首页标题:$appName"

echo ""
echo "c.************发布/调试微信小程序需要在微信开发者工具修改为微信小程序的appID:$appId**********"
echo "c.************发布/调试微信小程序需要在微信开发者工具修改为微信小程序的appID:$appId**********"
echo "c.************发布/调试微信小程序需要在微信开发者工具修改为微信小程序的appID:$appId**********"
echo ""
echo "微信小程序appID:$appId"
echo "微信小程序名称:$appName"
echo "微信小程序接口域名:$domainName"
echo "微信小程序发布时间:$curent_time"

echo ""
echo '配置完成'
read -p "回车键推出......" choice




